<?php
/*
	Version   : 1.2
	Created   : 20090106
	Modified  : 20090927
	Author    : Pablo Ramírez
	co-Author :
	Descr     : Controlador "Searchs"
	Changelog : 1.0 - Version Inicial
				1.1 - Se quita busqueda por owner
				1.2 - Control de Acceso
*/
class SearchsController extends AppController
{
	var $name       = 'Searchs';
	var $uses       = array('Order','Owner', 'Depot', 'OrderContainer');
	var $helpers    = array('Html','Form','Javascript','Ajax');
	
	function index() {
		$this->checkSession();
		$this->set('variable','hola');
		if (!empty($this->data)) {
			//$owner = $this->data['Search']['owner'];
			$container = $this->data['Search']['container'];
			/*if ($owner=='') {
				$owner=0;
			}*/
			$owner=0;
			if ($container=='') {
				$container=0;
			}
			$this->redirect('searchs/result/'. $owner .'/'. $container);
		}
	}
	
	function result($owner = null, $container = null) {
		$this->checkSession();		
		switch (true) 
		{ 
		    case ($owner=='0' and $container=='0'): 
				$this->Session->setFlash('Nothing to search');
		        $this->redirect('/searchs');
		        break; 
		    case ($owner!='0' and  $container=='0'): 
			$this->Session->setFlash('Search by owner ' . $owner);	
		        $this->set('results', $this->Order->findAll("Owner.own_nombre like '%$owner%'"));
		        break; 
		    case ($owner=='0' and  $container!='0'): 
				$this->Session->setFlash('Search by container '. $container);
				
				$cod_codigo_base = $this->getBaseCodCodigo($container);
				$order_containers = $this->OrderContainer->findAll("OrderContainer.cod_codigo like '$cod_codigo_base%'");
				
				$results = array();
				$i = 0;
				foreach ($order_containers as $order_container){

					$ord_codigo = $order_container['OrderContainer']['ord_codigo'];
					$ord_estado = $this->Order->field("ord_estado","ord_codigo = '$ord_codigo'");
					$cod_codigo = $order_container['OrderContainer']['cod_codigo'];
					$cot_codigo = $order_container['OrderContainer']['cot_codigo'];
					$dep_codigo = $order_container['OrderContainer']['dep_codigo'];
					$dep_nombre = $this->Depot->field("dep_nombre","dep_codigo = '$dep_codigo'");
					$oco_surveydate = substr($order_container['OrderContainer']['oco_surveydate'], 0, 10);
					if (($ord_estado == 'I') or ($ord_estado == 'N') or ($ord_estado == 'R') or ($ord_estado == 'S')) {	
						//$tmp = $this->Order->findAll("Order.ord_codigo = '$ord_codigo'");
						//if ($i == 0) {
						array_push($results, array('ord_codigo' => $ord_codigo,
												   'cod_codigo' => $cod_codigo,
												   'cot_codigo' => $cot_codigo,
												   'dep_codigo' => $dep_codigo,
												   'dep_nombre' => $dep_nombre,
												   'oco_surveydate' => $oco_surveydate));
						/*} else {
							/*echo $i;
							echo '<br />';*/
							//print_r(array_keys($results[$i-1]['Order']));
							//echo '<br />';
							//if (array_key_exists($i-1, $results)){
							/*echo $tmp[0]['Order']['ord_codigo'];
							echo '<br />';*/
							//pr($results);
							//$a = count($results);
							/*echo $a; 
							echo '<br />';
							echo $results[$a-1]['Order']['ord_codigo'];
							echo '<br />';
							if ($tmp[0]['Order']['ord_codigo'] != $results[$a-1]['Order']['ord_codigo']) {
								/*echo "encontre igualdad";
								echo '<br />';
							} else {
								array_push($results, $tmp['0']);
							}
						}*/
						$i++;		
					}

				}
				//pr($results);
				$this->set('results',$results);
		        break; 
		    case  ($owner!='0' and  $container!='0'): 
				$this->Session->setFlash('Search by owner ' . $owner);
				$this->set('results', $this->Order->findAll("Owner.own_nombre = '%$owner%'"));
		        break;
		    default : 
				$this->Session->setFlash('ERROR');
		        $this->redirect('/searchs');
		        break; 
		}
	}
}
?>